package 集合;

import 动态数组.线性表.List;
import 动态链表.LinkedList;
import 集合.接口.Map;
import 集合.接口.Set;

import java.util.Iterator;

public class TreeMap<K extends Comparable<K>,V> implements Map<K,V> {

    private class Node{
        public K key;
        public V value;
        public Node left;
        public Node right;

        public Node(){
            this(null,null);
        }

        public Node(K key,V value){
            this.key = key;
            this.value = value;
        }
    }

    private Node root;
    private int size;

    public TreeMap() {
        root = null;
        size = 0;
    }

    //以node为根节点，查找以key为键的节点对象
    private Node getNode(Node node,K key){
        if (node == null){
            return null;
        }
        if (key.compareTo(node.key) < 0){
            return getNode(node.left,key);
        }else if (key.compareTo(node.key) > 0){
            return getNode(node.right,key);
        }else {
            return node;
        }
    }

    @Override
    public void put(K key, V value) {
        root = put(root,key,value);
    }

    private Node put(Node node, K key, V value) {
        if (node == null){
            size++;
            return new Node(key,value);
        }
        if (key.compareTo(node.key) < 0){
            node.left = put(node.left,key,value);
        }else if(key.compareTo(node.key) > 0){
            node.right = put(node.right,key,value);
        }else {
            node.value = value;
        }
        return node;
    }

    @Override
    public V remove(K key) {
        Node delNode = getNode(root, key);
        if (delNode != null){
            root = remove(root,key);
            return delNode.value;
        }
        return null;
    }

    private Node remove(Node node, K key) {
        if (node == null){
            return null;
        }
        Node retNode = node;
        if (key.compareTo(node.key) < 0){
            node.left = remove(node.left,key);
        }else if (key.compareTo(node.key) > 0){
            node.right = remove(node.right,key);
        }else {
            if (node.left == null){
                Node rightNode = node.right;
                node.right = null;
                size--;
                retNode = rightNode;
            }else if (node.right == null){
                Node leftNode = node.left;
                node.left = null;
                size--;
                retNode = leftNode;
            }else {
                Node s = maximum(node.left);
                s.left = removeMax(node.left);
                s.right = node.right;
                node.right = node.left = null;
                retNode = s;
            }
        }
        return retNode;
    }

    private Node removeMax(Node node) {
        if (node.right == null){
            Node leftNode = node.left;
            node.left = null;
            size--;
            return leftNode;
        }
        node.right = removeMax(node.right);
        return node;
    }

    private Node maximum(Node node) {
        if (node.right == null){
            return node;
        }
        return maximum(node.right);
    }

    @Override
    public boolean contains(K key) {
        return getNode(root,key) != null;
    }

    @Override
    public V get(K key) {
        Node n = getNode(root, key);
        return n == null ? null : n.value;
    }

    @Override
    public V set(K key, V value) {
        Node n = getNode(root, key);
        if (n == null){
            return null;
        }
        V ret = n.value;
        n.value = value;
        return ret;
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size == 0 && root == null;
    }

    @Override
    public Set<K> keySet() {
        TreeSet<K> set = new TreeSet<>();
        inOrderKeySet(root,set);
        return set;
    }

    private void inOrderKeySet(Node node, TreeSet<K> set) {
        if (node == null){
            return;
        }
        inOrderKeySet(node.left,set);
        set.add(node.key);
        inOrderKeySet(node.right,set);
    }

    @Override
    public List<V> values() {
        LinkedList<V> list = new LinkedList<>();
        inOrderValues(root,list);
        return list;
    }

    private void inOrderValues(Node node, LinkedList<V> list) {
        if (node == null){
            return;
        }
        inOrderValues(node.left,list);
        list.add(node.value);
        inOrderValues(node.right,list);
    }

    @Override
    public Set<Entry<K, V>> entrySet() {
        LinkedSet<Entry<K,V>> set = new LinkedSet();
        inOrderEntrySet(root,set);
        return set;
    }

    private void inOrderEntrySet(Node node, LinkedSet<Entry<K, V>> set) {
        if (node == null){
            return;
        }
        inOrderEntrySet(node.left,set);
        set.add(new BSTEntry<K,V>(node.key,node.value));
        inOrderEntrySet(node.right,set);

    }

    private class BSTEntry<K,V> implements Entry<K,V>{
        private K key;
        private V value;
        public BSTEntry(K key,V value){
            this.key = key;
            this.value = value;
        }

        @Override
        public K getKey() {
            return key;
        }

        @Override
        public V getValue() {
            return value;
        }

    }

    @Override
    public Iterator<Entry<K, V>> iterator() {
        return null;
    }
}
